<?php

class Interest extends Frank implements iFrank {

    //Properties
    protected $year;
    protected $description;
    protected $displayOrder;
    protected $categoryId;

    //Getters
    public function year() {
        return $this->year;
    }

    public function description() {
        return $this->description;
    }

    public function categoryId() {
        return $this->categoryId;
    }

    public function __construct($id, $name, $year, $description, $displayOrder, $categoryId) {
        $this->id = $id;
        $this->name = $name;
        $this->year = $year;
        $this->description = $description;
        $this->displayOrder = $displayOrder;
        $this->categoryId = $categoryId;
    }

    public static function getObject($row) {
        return new Interest($row["interest_id"], $row["interest_name"], $row["interest_year"], $row["interest_description"], $row["interest_display_order"], $row["category_id"]);
    }

    public static function getAll($toArray = false, $fields = null, $query = null, $db = null) {
        $query = 'select * from `' . INTEREST_TABLE . '`';
        return parent::getAll($toArray, $fields, $query, $db);
    }
    
    public static function getById($id, $toArray = false, $fields = null, $query = null, $db = null){
        $query = 'select * from `' . INTEREST_TABLE . '` where interest_id = '.$id;
        return parent::getById($id, $toArray, $fields, $query, $db);
    }

    public static function getAllOrderedByCategory($toArray = false, $fields = null, $query = null, $db = null) {
        $query = 'select * '
                . 'from  `' . INTEREST_TABLE . '` '
                . 'inner join `' . INTEREST_CATEGORY_TABLE . '` ON `' . INTEREST_TABLE . '`.category_id = `' . INTEREST_CATEGORY_TABLE . '`.category_id '
                . 'order by `' . INTEREST_CATEGORY_TABLE . '`.category_display_order';
        $results = self::getFromQuery($query);
        if ($results != null) {
            $myInterests = array();
            $category = null;
            $myCategoryLentgh = 0;
            foreach ($results as $row) {
                if ($category == null || $row["category_id"] != $category->id()) {
                    $category = InterestCategory::getObject($row);
                    $myCategoryLentgh = array_push($myInterests, array('category' => $category, 'interests' => array()));
                }
                $newInterest = self::getObject($row);
                array_push($myInterests[$myCategoryLentgh - 1]['interests'], $newInterest);
            }
            return self::tabObjectsToArray($myInterests, $toArray, $fields);
        }
        return null;
    }
    
    public static function getByIdOrderedByCategory($id, $toArray = false, $fields = null, $query = null, $db = null){
        $query = 'select * '
                . 'from  `' . INTEREST_TABLE . '` '
                . 'inner join `' . INTEREST_CATEGORY_TABLE . '` ON `' . INTEREST_TABLE . '`.category_id = `' . INTEREST_CATEGORY_TABLE . '`.category_id '
                . 'where interest_id = '.$id;
        $results = self::getFromQuery($query);
        if ($results != null) {
            $category = InterestCategory::getObject($results[0]);
            $myInterest = self::getObject($results[0]);
            $myInterests[] = array('category' => $category, 'interests' => array($myInterest));
            return self::tabObjectsToArray($myInterests, $toArray, $fields);
        }
        return null;
    }

    private static function tabObjectsToArray($myCategories, $toArray = false, $fields = null) {
        if ($toArray) {
            if (isset($myCategories) && count($myCategories) > 0) {
                $myCategoriesTab = array();
                foreach ($myCategories as $category) {
                    $categoryTab = $category['category']->toArray($fields);
                    $interestsTab = array();
                    foreach ($category['interests'] as $interest) {
                        array_push($interestsTab, $interest->toArray($fields));
                    }
                    $categoryTab['interests'] = $interestsTab;
                    array_push($myCategoriesTab, $categoryTab);
                }
                return $myCategoriesTab;
            }
        }
        return $myCategories;
    }

}

?>